import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkColumnName = async (_rule: dataSourceType = {}, value: any) => {
  if (!value) {
    return Promise.reject(`${_rule.msg1}`);
  } else {
    var params = Object.assign(_rule.params, {
      name: value,
    })
    if (_rule.api) {

      let res = await request[`${_rule.api}`](params);
      if (!res) {
        return Promise.reject(`${_rule.msg2}`);
      } else {
        return Promise.resolve();
      }
    }

  }
};

export const checkPath = (_rule: dataSourceType, value: string, callback: () => {}, msg = '请输入英文、数字、下划线', useValue: number = 0) => {
  if (_rule.required) {// 必填项验证非空并排除非必须项
    if (!useValue) { // 使用fullField校验
      if ((value === '' || value === null || value === undefined)) {
        return Promise.reject(`访问路径不能为空！`);
      } else {
        const reg = /^[_|A-Za-z0-9]+$/;
        return reg.test(value) ? Promise.resolve() : msg ? Promise.reject(`${msg}`) : Promise.reject(`${_rule.msg}`);
      }
    } else {
      if (!value || value == undefined) {
        return Promise.reject(`${msg}`);
      } else {
        const reg = /^[A-Za-z0-9]+$/;
        return reg.test(value) ? Promise.resolve() : msg ? Promise.reject(`${msg}`) : Promise.reject(`${msg}`);
      }
    }
  } else {
    if (!useValue) {
      if ((value === '' || value === null || value === undefined)) {
        return Promise.resolve();
      } else {
        const reg = /^[A-Za-z0-9]+$/;
        return reg.test(value) ? Promise.resolve() : msg ? Promise.reject(`${msg}`) : Promise.reject(`${msg}`);
      }
    } else {
      if (!value || value == undefined) {
        return Promise.resolve();
      } else {
        const reg = /^[A-Za-z0-9]+$/;
        return reg.test(value) ? Promise.resolve() : msg ? Promise.reject(`${msg}`) : Promise.reject(`${msg}`);
      }
    }
  }
};

export const checkOrgContent = async (_rule: dataSourceType = {}, value: any) => {
  if (!value) {
    return Promise.resolve();
  } else {
    if (_rule.api) {
      let res = await request[`${_rule.api}`](_rule.params);
      if (res.contentExists === true) {
        return Promise.reject(`${_rule.msg1}`);
      } else {
        return Promise.resolve();
      }
    }

  }
};